// var fs = require("fs");
// var sql = require('sql.js');
// var db = new sql.Database(fs.readFileSync("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite"));
// var obj = {
//     name: "加上",
//     age: 20,
//     sex: {
//         h:[1,2,3]
//     }
// }
// b = JSON.stringify(obj)
// db.run(`insert into test (name,age) values ('${b}',25);`)
// fs.writeFile("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite",new Buffer(db.export()))

//查询返回对象
// var fs = require("fs");
// var sql = require('sql.js');
// var db = new sql.Database(fs.readFileSync("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite"));
//
// var a = db.prepare(`select * from test where id=:id`)
// var result = a.getAsObject({':id' : 10});
// console.log(result)



//先删除再插入
// var fs = require("fs");
// var sql = require('sql.js');
// var db = new sql.Database(fs.readFileSync("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite"));
// var obj = {
//     a: 165,
//     b: "李江",
//     c: 265893455
// }
// JSONobj = JSON.stringify(obj)
// var a = db.exec(`select count(*) from sqlite_master where type='table' and name='${obj.b+obj.c}'`)[0].values
// console.log(a[0][0])
// var sqlstr = a[0][0] > 0 ? `` : `create table ${obj.b+obj.c} (
//     id integer primary key autoincrement not null,
//     cid int,
//     name text,
//     sex int);`
// sqlstr+=`insert into ${obj.b+obj.c} (cid,name,sex) values (${obj.a},'${obj.b}',${obj.c})`
// db.run(`DELETE FROM session where accid=${obj.c}`)
// db.run(`insert into session (accid,json) values (${obj.c},'${JSONobj}')`);
// db.run(sqlstr)
//
//
//
// fs.writeFile("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite",new Buffer(db.export()))


//正序，倒序，查询多少条
// var fs = require("fs");
// var sql = require('sql.js');
// var db = new sql.Database(fs.readFileSync("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite"));
//
//
// var c = db.exec(`SELECT * FROM 李江265893455 order by id desc LIMIT 15,5 ;`)
// console.log(c[0].values)



//先查询有没有，无则添，有则不插
var fs = require("fs");
var sql = require('sql.js');
var db = new sql.Database(fs.readFileSync("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite"));
var obj = {
    a: 165,
    b: "李江",
    c: 265893455
}
JSONobj = JSON.stringify(obj)
var a = db.exec(`select count(*) from sqlite_master where type='table' and name='${obj.b+obj.c}'`)[0].values
console.log(a[0][0])
var sqlstr = a[0][0] > 0 ? `` : `create table ${obj.b+obj.c} (
    id integer primary key autoincrement not null,
    cid int,
    name text,
    sex int);`
sqlstr+=`insert into ${obj.b+obj.c} (cid,name,sex) values (${obj.a},'${obj.b}',${obj.c})`
db.run(`DELETE FROM session where accid=${obj.c}`)
db.run(`insert into session (accid,json) values (${obj.c},'${JSONobj}')`);
db.run(sqlstr)



fs.writeFile("0b08fcf0ad3eae8620ea56e0daa457ef.sqlite",new Buffer(db.export()))





